package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.smalls.newvideotwo.mvp.bean.StreamInfo;
import com.smalls.redshoes.R;

import java.util.ArrayList;

import cn.bingoogolapple.androidcommon.adapter.BGAAdapterViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;


public class PlayBarSelectVodLayout extends AutoHideLayout implements OnItemClickListener, OnKeyListener {
    public static final int AVERAGE_COUNT = 8;
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + PlayBarSelectVodLayout.class.getSimpleName():PlayBarSelectVodLayout.class.getSimpleName();
    private SelectVodAdapter mAdapter;
    private ListView mListView;
    private RSRecyclerAdapter mRSRecyclerAdapter;
    private OnStreamingClickedListener onStreamingClickedListener;
    private RecyclerView rv_select_episode;
    private int selection;
    private ArrayList<StreamInfo> streamingModels;

    public interface OnStreamingClickedListener {
        void onStreamingClicked(int i);
    }

    class SelectVodAdapter extends BGAAdapterViewAdapter<StreamInfo> {
        public SelectVodAdapter(Context context, int itemLayoutId) {
            super(context, itemLayoutId);
        }

        protected void fillData(BGAViewHolderHelper viewHolderHelper, int i, StreamInfo streamingModel) {
            if (streamingModel != null) {
                viewHolderHelper.setText(R.id.sequence_tv, streamingModel.getSequence() + "");
            }
        }
    }

    private class RSRecyclerAdapter extends BGARecyclerViewAdapter<StreamInfo> {
        public RSRecyclerAdapter(RecyclerView recyclerView, int itemLayoutId) {
            super(recyclerView, itemLayoutId);
        }

        protected void fillData(BGAViewHolderHelper viewHolderHelper, int position, StreamInfo model) {
            if (model != null) {
                viewHolderHelper.setText(R.id.sequence_tv, String.format(PlayBarSelectVodLayout.this.getResources().getString(R.string.rs_vod_play_episode), new Object[]{Integer.valueOf(model.getSequence())}));
                viewHolderHelper.getConvertView().setOnFocusChangeListener(new OnFocusChangeListener() {
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus) {
                            PlayBarSelectVodLayout.this.delayHide();
                        }
                    }
                });
            }
        }
    }

    public void setOnStreamingClickedListener(OnStreamingClickedListener onStreamingClickedListener) {
        this.onStreamingClickedListener = onStreamingClickedListener;
    }

    public PlayBarSelectVodLayout(Context context) {
        this(context, null);
    }

    public PlayBarSelectVodLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayBarSelectVodLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.selection = 0;
        init(context);
    }

    protected void init(Context context) {
        setAutoHide(true);
        super.init(context);

        this.rv_select_episode = (RecyclerView) LayoutInflater.from(context).inflate(R.layout.rs_layout_vod_play_select_vod, this, true).findViewById(R.id.rv_select_episode);
        this.rv_select_episode.setLayoutManager(new LinearLayoutManager(context, 0, false));
        this.mRSRecyclerAdapter = new RSRecyclerAdapter(this.rv_select_episode, R.layout.rs_item_vod_play_select_bar_list);
        this.rv_select_episode.setAdapter(this.mRSRecyclerAdapter);
        this.mRSRecyclerAdapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() {
            public void onRVItemClick(ViewGroup parent, View itemView, int position) {
                if (PlayBarSelectVodLayout.this.onStreamingClickedListener != null) {
                    PlayBarSelectVodLayout.this.onStreamingClickedListener.onStreamingClicked(position);
                }
            }
        });
        return;
    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == 0) {
            delayHide();
        }
        return false;
    }

    public void setDatas(ArrayList<StreamInfo> streamingModels) {
        this.streamingModels = streamingModels;
        if (this.mRSRecyclerAdapter != null) {
            this.mRSRecyclerAdapter.setData(this.streamingModels);
        }
    }

    public void setSelection(int position) {
        this.selection = position;
        if (this.mListView != null && this.mAdapter != null && this.mAdapter.getCount() > 0) {
            this.mListView.setSelection(position);
        }
    }

    public void updateVisibility() {
        setVisibility(getVisibility() == View.VISIBLE ? GONE :VISIBLE);
    }

    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (this.onStreamingClickedListener != null) {
            this.onStreamingClickedListener.onStreamingClicked(position);
        }
    }

    public void setVisibility(int visibility) {
        if (this.streamingModels == null || this.streamingModels.size() <= 1) {
            super.setVisibility(GONE);
        } else {
            super.setVisibility(visibility);
        }

        if (visibility == 0 && this.rv_select_episode != null && this.streamingModels != null && this.streamingModels.size() > 1) {
            this.rv_select_episode.scrollToPosition(this.selection);
            this.rv_select_episode.post(new Runnable() {
                public void run() {
                    PlayBarSelectVodLayout.this.setSelection(PlayBarSelectVodLayout.this.selection);
                    if (PlayBarSelectVodLayout.this.rv_select_episode != null && PlayBarSelectVodLayout.this.mRSRecyclerAdapter != null && PlayBarSelectVodLayout.this.mRSRecyclerAdapter.getItemCount() > 0) {
                        ViewHolder viewHolder = PlayBarSelectVodLayout.this.rv_select_episode.findViewHolderForLayoutPosition(PlayBarSelectVodLayout.this.selection);
                        if (viewHolder != null) {
                            viewHolder.itemView.requestFocus();
                        }
                    }
                }
            });
        }
    }
}
